using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DataAccessLayer
{
    public class DAO_Account_Role_Map
    {
        public static bool Insert(Object tmp)
        {
            ACCOUNT_ROLE_MAP obj = (ACCOUNT_ROLE_MAP)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            _list.Add(new SqlParameter("@ACCOUNT_ID", obj.ACCOUNT_ID));
            _list.Add(new SqlParameter("@ACCOUNT_ROLE_MAP_GRANTED_DATE", obj.ACCOUNT_ROLE_MAP_GRANTED_DATE));
            _list.Add(new SqlParameter("@_TABLE__PK", obj._TABLE__PK));
            _list.Add(new SqlParameter("@ROLE_ID", obj.ROLE_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_account_role_map_insert", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static bool Update(Object tmp)
        {
            ACCOUNT_ROLE_MAP obj = (ACCOUNT_ROLE_MAP)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            _list.Add(new SqlParameter("@ACCOUNT_ID", obj.ACCOUNT_ID));
            _list.Add(new SqlParameter("@ACCOUNT_ROLE_MAP_GRANTED_DATE", obj.ACCOUNT_ROLE_MAP_GRANTED_DATE));
            _list.Add(new SqlParameter("@_TABLE__PK", obj._TABLE__PK));
            _list.Add(new SqlParameter("@ROLE_ID", obj.ROLE_ID));
            _list.Add(new SqlParameter("@ACCOUNT_ROLE_MAP_ID", obj.ACCOUNT_ROLE_MAP_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_account_role_map_update", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static bool Delete(Object tmp)
        {
            ACCOUNT_ROLE_MAP obj = (ACCOUNT_ROLE_MAP)tmp;
            bool result = false;
            List<SqlParameter> _list = new List<SqlParameter>();
            //_list.Add(new SqlParameter("@ACCOUNT_ID", obj.ACCOUNT_ID));
            //_list.Add(new SqlParameter("@ACCOUNT_ROLE_MAP_GRANTED_DATE", obj.ACCOUNT_ROLE_MAP_GRANTED_DATE));
            //_list.Add(new SqlParameter("@_TABLE__PK", obj._TABLE__PK));
            //_list.Add(new SqlParameter("@ROLE_ID", obj.ROLE_ID));
            _list.Add(new SqlParameter("@ACCOUNT_ROLE_MAP_ID", obj.ACCOUNT_ROLE_MAP_ID));

            int n = clsDataAccessHelper.ExecNonQuery("sp_account_role_map_delete", _list);
            if (n == 1)
                result = true;
            return result;
        }

        public static List<ACCOUNT_ROLE_MAP> LoadAll(string whereStr)
        {

            List<ACCOUNT_ROLE_MAP> _list = new List<ACCOUNT_ROLE_MAP>();
            //call stored getlist here
            List<SqlParameter> _listPara = new List<SqlParameter>();
            _listPara.Add(new SqlParameter("@WhereString", whereStr));
            DataTable dt = clsDataAccessHelper.ExecuteQuery("spSelectAccount_role_map", _listPara);
            foreach (DataRow dr in dt.Rows)
            {
                ACCOUNT_ROLE_MAP obj = new ACCOUNT_ROLE_MAP();
                obj._TABLE__PK = dr["_TABLE__PK"].ToString();
                obj.ACCOUNT_ID = dr["ACCOUNT_ID"].ToString();
                obj.ACCOUNT_ROLE_MAP_GRANTED_DATE = Convert.ToDateTime(dr["ACCOUNT_ROLE_MAP_GRANTED_DATE"].ToString());
                obj.ACCOUNT_ROLE_MAP_ID = dr["ACCOUNT_ROLE_MAP_ID"].ToString();
                obj.ROLE_ID = Convert.ToDecimal(dr["ROLE_ID"].ToString());
                
                _list.Add(obj);
            }
            return _list;
        }

        public static int checkRole(string accID, int roleID )
        {
            String sql = "select count(*) AS COUNT from ACCOUNT_ROLE_MAP where ACCOUNT_ID = '" + accID + "' and ROLE_ID = " + roleID;
            DataTable dt = clsDataAccessHelper.ExecuteQueryNonStoredProcedure(sql, null);
            return Convert.ToInt32(dt.Rows[0][0]);
        }
    }
}
